﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace VIT.Provider
{
    public class WebConfig : WebConfig_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Constructor
        public WebConfig(  
            string LanguageId = "",
            string Email = "",
            string PreTemplate = "",
            string AdminTemplate = "",
            string MailServer="")
        {
            this.LanguageId = LanguageId;
            this.PreTemplate = PreTemplate;
            this.AdminTemplate = AdminTemplate;
            this.ModifyByUser = MailServer;
            this.ModifyDate = DateTime.Now;
        }
        #endregion

        #region
        public DataTable GetAll(bool isTransaction = false)
        {
            string strQuery = "select PreTemplate, AdminTemplate, Name from WebConfig, Language where LanguageId=Language.Id";
            return ExecuteDataset(isTransaction, strQuery).Tables[0];
        }
        public DataTable GetConfig(bool isTransaction = false)
        {
            string strQuery = "select WebConfig.*, Name from WebConfig, Language where LanguageId=Language.Id";
            return ExecuteDataset(isTransaction, strQuery).Tables[0];
        }
        #endregion

        #region Edit
        public void GetSingle(bool isTransaction = false)
        {
            string strQuery = "select * from WebConfig where LanguageID=@LanguageId";
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = ExecuteDataset(isTransaction, strQuery, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.PreTemplate = dt.Rows[0]["PreTemplate"].ToString();
                this.AdminTemplate = dt.Rows[0]["AdminTemplate"].ToString();
            }
        }

        public int UpdateObj(bool isTransaction = false)
        {
            string sqlCat = "update WebConfig set Email=@Email, ModifyByUser=@ModifyByUser, ModifyDate=@ModifyDate, PreTemplate=@PreTemplate, AdminTemplate=@AdminTemplate, ModifyDate=@Now, ModifyByUser=@User where LanguageId=@LanguageId";
            SqlParameter PreTemplate = new SqlParameter("@PreTemplate", this.PreTemplate);
            SqlParameter AdminTemplate = new SqlParameter("@AdminTemplate", this.AdminTemplate);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter ModifyByUser = new SqlParameter("@ModifyByUser", user);
            SqlParameter ModifyDate = new SqlParameter("@ModifyDate", DateTime.Now);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            HttpContext.Current.Application["ModuleData"] = null;
            return ExecuteNonQuery(isTransaction, sqlCat, ModifyByUser, ModifyDate, PreTemplate, AdminTemplate, LanguageId, User, Now);
        }
        #endregion
    }
}
